from rest_framework.serializers import ModelSerializer
from rest_framework import serializers
from work.models import Label, Topic


class LabelSerializer(ModelSerializer):
    username = serializers.CharField(source='user.username', read_only=True)

    class Meta:
        model = Label
        fields = '__all__'
        extra_kwargs = {
            'user': {'write_only': True}
        }


class TopicSerializer(ModelSerializer):
    label_name = serializers.CharField(source='label.name', read_only=True)
    username = serializers.CharField(source='user.username', read_only=True)

    class Meta:
        model = Topic
        exclude = ['is_delete']
        extra_kwargs = {
            'user': {'write_only': True},
            'label': {'write_only': True},
        }


class TopicNoAnswerSerializer(ModelSerializer):
    label_name = serializers.CharField(source='label.name', read_only=True)
    username = serializers.CharField(source='user.username', read_only=True)

    class Meta:
        model = Topic
        exclude = ['is_delete', 'answer']
        extra_kwargs = {
            'user': {'write_only': True},
            'label': {'write_only': True},
        }


class LabelTopicSerializer(ModelSerializer):
    topic_set = TopicNoAnswerSerializer(many=True, required=False, read_only=True)

    class Meta:
        model = Label
        fields = '__all__'
